﻿using ZYCommon;
using System;
using System.Collections;
using System.Data;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Forms;

namespace TextEditor
{
    

    public class ZYKBListView : ListView
    {
        private bool bolShowChildKBList = false;
        private ZYKBBuffer myKBBuffer = null;
        private KB_List myOwnerKBList = null;
        private string strDefaultStatus = null;

        public event StatusTextChangeHandler StatusTextChange;

        public ZYKBListView()
        {
            ColumnHeader header = new ColumnHeader();
            header.Text = "项目文本";
            header.Width = 150;
            base.Columns.Add(header);
            header = new ColumnHeader();
            header.Text = "项目值";
            header.Width = 150;
            base.Columns.Add(header);
            header = new ColumnHeader();
            header.Text = "类型";
            header.Width = 90;
            base.Columns.Add(header);
            header = new ColumnHeader();
            header.Text = "编号";
            header.Width = 80;
            base.Columns.Add(header);
            base.FullRowSelect = true;
            base.GridLines = true;
            base.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            base.HideSelection = false;
            base.LabelEdit = true;
            base.View = View.Details;
        }

        private void Alert(string strAlert)
        {
            MessageBox.Show(this, strAlert, "系统警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private ListViewItem CreateListViewItem()
        {
            ListViewItem item = new ListViewItem();
            item.SubItems.Add("");
            item.SubItems.Add("");
            item.SubItems.Add("");
            base.Items.Add(item);
            return item;
        }

        public void DeleteSelectedItems()
        {
            foreach (ListViewItem item in base.SelectedItems)
            {
                if (item.Tag is KB_Item)
                {
                    KB_Item tag = item.Tag as KB_Item;
                    if (!tag.isCreated())
                    {
                        tag.DataState = DataRowState.Deleted;
                        item.ImageIndex = ZYKBTreeView.GetICONIndex(tag, false);
                    }
                }
                else
                {
                    KB_List myRecord = item.Tag as KB_List;
                    myRecord.DataState = DataRowState.Deleted;
                    item.ImageIndex = ZYKBTreeView.GetICONIndex(myRecord, false);
                }
                item.BackColor = ZYKBTreeView.GetItemBackColor(item.Tag as ZYDBRecordBase);
            }
        }

        public bool EditFocusedItem()
        {
            if ((base.FocusedItem != null) && (base.FocusedItem.Tag is KB_Item))
            {
                //using (dlgKB_Item item = new dlgKB_Item())
                //{
                //    item.OwnerItem = base.FocusedItem.Tag as KB_Item;
                //    if (item.ShowDialog(base.FindForm()) == DialogResult.OK)
                //    {
                //        this.RefreshListItem(base.FocusedItem);
                //        return true;
                //    }
                //}
            }
            return false;
        }

        public bool ItemMoveDown()
        {
            return this.MoveKBListItem(1);
        }

        public bool ItemMoveUp()
        {
            return this.MoveKBListItem(-1);
        }

        private bool MoveKBListItem(int intStep)
        {
            if (base.SelectedItems.Count > 0)
            {
                int num = base.SelectedItems[0].Index;
                ArrayList childNodes = null;
                if (this.ShowChildKBList)
                {
                    childNodes = this.myOwnerKBList.ChildNodes;
                }
                else
                {
                    childNodes = this.myOwnerKBList.Items;
                }
                if (((num + intStep) >= 0) && ((num + intStep) < childNodes.Count))
                {
                    int listIndex = 0;
                    IListIndex index = childNodes[num] as IListIndex;
                    IListIndex index2 = childNodes[num + intStep] as IListIndex;
                    if ((index is KB_Item) && ((index as KB_Item).isCreated() || (index2 as KB_Item).isCreated()))
                    {
                        return false;
                    }
                    childNodes.Remove(index2);
                    childNodes.Insert(num, index2);
                    listIndex = index.ListIndex;
                    index.ListIndex = index2.ListIndex;
                    index2.ListIndex = listIndex;
                    this.RefreshContent();
                    base.Items[num + intStep].Selected = true;
                    base.Items[num + intStep].Focused = true;
                    base.Items[num + intStep].EnsureVisible();
                    return true;
                }
            }
            return false;
        }

        public bool NewKBItem()
        {
            if ((this.myOwnerKBList != null) && !this.myOwnerKBList.HasChildren())
            {
                //string strData = dlgInputBox.InputBox("输入新增的项目文本", "新增项目", "");
                //if (!StringCommon.isBlankString(strData))
                //{
                //    KB_Item item = this.myOwnerKBList.NewItem(strData);
                //    ListViewItem myItem = this.CreateListViewItem();
                //    myItem.Tag = item;
                //    this.RefreshListItem(myItem);
                //    return true;
                //}
            }
            return false;
        }

        public bool NewKBList()
        {
            if ((this.myOwnerKBList != null) && !this.myOwnerKBList.HasItems())
            {
                //string strData = dlgInputBox.InputBox("输入新的列表的名称", "新增列表", "");
                //if (!StringCommon.isBlankString(strData))
                //{
                //    KB_List list = this.myOwnerKBList.NewChildList(strData);
                //    ListViewItem myItem = this.CreateListViewItem();
                //    myItem.Tag = list;
                //    this.RefreshListItem(myItem);
                //    return true;
                //}
            }
            return false;
        }

        protected override void OnAfterLabelEdit(LabelEditEventArgs e)
        {
            if (e.Label != null)
            {
                if (base.Items[e.Item].Tag is KB_Item)
                {
                    KB_Item tag = base.Items[e.Item].Tag as KB_Item;
                    if ((tag.ItemValue == null) || tag.ItemText.Equals(tag.ItemValue))
                    {
                        if (!KB_Item.CheckItemText(e.Label))
                        {
                            this.Alert("列表项目文本不合法，不得为空且长度不得超过100个字符");
                        }
                        tag.ItemText = e.Label;
                        if (tag.ItemValue != null)
                        {
                            tag.ItemValue = tag.ItemText;
                        }
                    }
                    else
                    {
                        tag.ItemText = e.Label;
                    }
                    this.RefreshListItem(base.Items[e.Item]);
                }
                else
                {
                    KB_List list = base.Items[e.Item].Tag as KB_List;
                    if (!KB_List.CheckName(e.Label))
                    {
                        this.Alert("知识点名称不合法,不得为空且长度不得超过50个字符");
                    }
                    list.Name = e.Label;
                    this.RefreshListItem(base.Items[e.Item]);
                }
                base.OnAfterLabelEdit(e);
            }
        }

        protected override void OnBeforeLabelEdit(LabelEditEventArgs e)
        {
            if (base.Items[e.Item].Tag is KB_Item)
            {
                KB_Item tag = base.Items[e.Item].Tag as KB_Item;
                if ((tag.ItemStyle == 1) || tag.isDeleted())
                {
                    e.CancelEdit = true;
                }
                else
                {
                    ZYDBRecordBase base2 = base.Items[e.Item].Tag as ZYDBRecordBase;
                    if (base2.isDeleted())
                    {
                        e.CancelEdit = true;
                    }
                    else
                    {
                        base.OnBeforeLabelEdit(e);
                    }
                }
            }
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            if ((base.LabelEdit && (base.FocusedItem != null)) && (base.FocusedItem.Tag is KB_Item))
            {
                //using (dlgKB_Item item = new dlgKB_Item())
                //{
                //    item.OwnerItem = base.FocusedItem.Tag as KB_Item;
                //    if (item.ShowDialog(base.FindForm()) == DialogResult.OK)
                //    {
                //        this.RefreshListItem(base.FocusedItem);
                //    }
                //    return;
                //}
            }
            base.OnDoubleClick(e);
        }

        protected override void OnItemDrag(ItemDragEventArgs e)
        {
            if (base.SelectedItems.Count > 0)
            {
                ArrayList data = new ArrayList();
                foreach (ListViewItem item in base.SelectedItems)
                {
                    data.Add(item.Tag);
                }
                DataObject obj2 = new DataObject(data);
                base.DoDragDrop(obj2, DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll);
            }
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            if (this.StatusTextChange != null)
            {
                if (base.SelectedItems.Count == 1)
                {
                    this.StatusTextChange(this, this.myOwnerKBList.GetRealFullPath() + "." + base.SelectedItems[0].Text);
                }
                else if (base.SelectedItems.Count > 1)
                {
                    this.StatusTextChange(this, "共选择 " + base.SelectedItems.Count + " 个对象");
                }
                else
                {
                    this.StatusTextChange(this, this.strDefaultStatus);
                }
            }
        }

        public void RefreshContent()
        {
            base.BeginUpdate();
            base.Items.Clear();
            int num = 0;
            if ((this.bolShowChildKBList && (this.myOwnerKBList != null)) && this.myOwnerKBList.HasChildren())
            {
                foreach (KB_List list in this.myOwnerKBList.ChildNodes)
                {
                    ListViewItem myItem = this.CreateListViewItem();
                    myItem.Tag = list;
                    this.RefreshListItem(myItem);
                    if (list.Items != null)
                    {
                        num += list.Items.Count;
                    }
                }
            }
            if ((this.myOwnerKBList != null) && this.myOwnerKBList.HasItems())
            {
                foreach (KB_Item item2 in this.myOwnerKBList.Items)
                {
                    ListViewItem item3 = this.CreateListViewItem();
                    item3.Tag = item2;
                    this.RefreshListItem(item3);
                }
            }
            base.EndUpdate();
            if ((this.StatusTextChange != null) && (this.myOwnerKBList != null))
            {
                if (this.myOwnerKBList == null)
                {
                    this.StatusTextChange(this, null);
                }
                else
                {
                    StringBuilder builder = new StringBuilder();
                    builder.Append("[" + this.myOwnerKBList.GetRealFullPath() + "]");
                    builder.Append(" 共 " + base.Items.Count + " 个对象");
                    if (this.myOwnerKBList.HasChildren())
                    {
                        builder.Append(" 有 " + this.myOwnerKBList.ChildNodes.Count + " 个知识点 ");
                    }
                    if (num > 0)
                    {
                        builder.Append("拥有 " + num.ToString() + " 个列表项目");
                    }
                    this.strDefaultStatus = builder.ToString();
                    this.StatusTextChange(this, this.strDefaultStatus);
                }
            }
        }

        private void RefreshListItem(ListViewItem myItem)
        {
            if (myItem.Tag is KB_Item)
            {
                KB_Item tag = myItem.Tag as KB_Item;
                if (base.SmallImageList != null)
                {
                    myItem.ImageIndex = ZYKBTreeView.GetICONIndex(tag, false);
                }
                myItem.Text = tag.ItemText;
                myItem.SubItems[1].Text = tag.ItemValue;
                myItem.SubItems[2].Text = KB_Item.KBItemStyleToDesc(tag.ItemStyle);
                myItem.SubItems[3].Text = tag.ItemSEQ.ToString();
            }
            else
            {
                KB_List myRecord = myItem.Tag as KB_List;
                if (base.SmallImageList != null)
                {
                    myItem.ImageIndex = ZYKBTreeView.GetICONIndex(myRecord, false);
                }
                myItem.Text = myRecord.Name;
                string str = null;
                if (myRecord.ChildNodes != null)
                {
                    str = myRecord.ChildNodes.Count.ToString() + "个子节点";
                }
                if (myRecord.Items != null)
                {
                    str = myRecord.Items.Count.ToString() + "个项目";
                }
                myItem.SubItems[1].Text = str;
                if (StringCommon.isBlankString(myRecord.ListSQL))
                {
                    myItem.SubItems[2].Text = "知识点";
                }
                else
                {
                    myItem.SubItems[2].Text = "SQL知识点";
                }
                myItem.SubItems[3].Text = myRecord.SEQ;
            }
            myItem.BackColor = ZYKBTreeView.GetItemBackColor(myItem.Tag as ZYDBRecordBase);
            if (this.myKBBuffer != null)
            {
                myItem.ForeColor = this.myKBBuffer.GetItemForeColor(myItem.Tag as KB_List);
            }
        }

        public ZYKBBuffer KBBuffer
        {
            get
            {
                return this.myKBBuffer;
            }
            set
            {
                this.myKBBuffer = value;
            }
        }

        public KB_List OwnerKBList
        {
            get
            {
                return this.myOwnerKBList;
            }
            set
            {
                this.myOwnerKBList = value;
                this.RefreshContent();
            }
        }

        public bool ShowChildKBList
        {
            get
            {
                return this.bolShowChildKBList;
            }
            set
            {
                this.bolShowChildKBList = value;
            }
        }
    }
}

